package com.jojo.android.zxlib.tool;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

/**
 * 创建者 zx
 * 创建时间 2020/8/17 10:33
 */
public class ZxHttpTool {


    public static void Get(String url, final OnCallback callback) {
        OkHttpClient okHttpClient = new OkHttpClient();
        Request request = new Request.Builder()
                .get()
                .url(url)
                .build();
        okHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                if (callback != null) {
                    callback.onErr(-1, e.getMessage());
                }
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if (callback != null) {
                    callback.onResponse(response.body().string());
                }
            }
        });
    }


    public interface OnCallback {
        void onResponse(String response);

        void onErr(int code, String err);
    }

}
